<script src="http://maps.google.com/maps/api/js?sensor=false"
	type="text/javascript"></script>

<script type="text/javascript">
$(document).ready(function(){
    $('#state').change(function(){ //any select change on the dropdown with id country trigger this code
        $("#city > option").remove(); //first of all clear select items
        var stateid = $('#state').val();  // here we are taking country id of the selected one.
        //alert(state_id)
         $("#city").removeAttr('disabled');
       
        var base_url = $('body').data('baseurl');
		//alert(base_url);
        
        
        //var base_url = "<?php echo 'HELL'; ?>";
        //alert (base_url);
        
        
        $.ajax({
            type: "POST",
            url: base_url+"location/locations/get_cities/"+stateid, //here we are calling our user controller and get_cities method with the country_id
            
            success: function(cities) //we're calling the response json array 'cities'
            {
            	var opt1 = $('<option />'); // here we're creating a new select option with for each city
                opt1.val(0);
                opt1.text("--Select City--");
                $('#city').append(opt1); //here we will append these new select options to a dropdown with the id 'cities'
                
            	
                $.each(cities,function(id,city) //here we're doing a foeach loop round each city with id as the key and city as the value
                {
                	var opt = $('<option />'); // here we're creating a new select option with for each city
                    opt.val(id);
                    opt.text(city);
                    $('#city').append(opt); //here we will append these new select options to a dropdown with the id 'cities'
                });
            }

        });

    });
});


function howMany() {
    var selObj = document.getElementById('keyword');
    var totalChecked = 0;
    for (i = 0; i < selObj.options.length; i++) {
        if (selObj.options[i].selected) {
            totalChecked++;
        }
    }
    
    if (totalChecked > 3) {
       alert("You can't select keyword more than 3 options");
       return false;
    }
    
    return true;
}
function testSubmit() {
return howMany();
}



function getlatlong() {
	$('#mymap').show();
	var geocoder = new google.maps.Geocoder();
	
	var street=$("#street_address").val();
	var street2=$("#street_address2").val();
	//var country=$("#country option:selected").html();
	var state=$("#statediv option:selected").html();
	var city=$("#citydiv option:selected").html();
	
	//var address=country+','+state+','+city+','+street;
	var address=street+','+street2+','+city+','+state;
	
	geocoder.geocode( { 'address': address}, function(results, status) {

	if (status == google.maps.GeocoderStatus.OK) {
    var latitude = results[0].geometry.location.lat();
	var longitude = results[0].geometry.location.lng();
	document.getElementById('lat').value=latitude;
	document.getElementById('long').value=longitude;
    } 

	
    var latlng = new google.maps.LatLng(latitude,longitude);
    var map = new google.maps.Map(document.getElementById('map'), {
        center: latlng,
        zoom: 14,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });
    var marker = new google.maps.Marker({
        position: latlng,
        map: map,
        title: 'Set lat/lon values for this property',
        draggable: true
    });
    google.maps.event.addListener(marker, 'dragend', function(a) {
        console.log(a);
       
        
		document.getElementById('lat').value = a.latLng.lat().toFixed(4);
		document.getElementById('long').value = a.latLng.lng().toFixed(4);
        document.getElementsByTagName('body')[0].appendChild(div);
    });
	}); 
};

</script>


<?php 
if($mode== "new"){

	?>
<style>
.inner {
	padding: 10px
}

#map {
	height: 300px;
	border: 1px solid #000;
}

.error {
	font-size: 10px;
	color: red;
	margin-left: 10px
}
</style>

<div class="separator"></div>

<h3 class="glyphicons briefcase">
	<i></i> Add New Listing
</h3>
<div class="separator"></div>



<form class="form-horizontal" style="margin-bottom: 0;" method="POST"
	action="<?= base_url();?>secure/listing/listings/submit/new">

<?php if($this->session->flashdata('message')) { 
				$show_message = $this->session->flashdata('message'); ?>
<span class="error"><?= $show_message; ?> </span>
<?php } ?>
	<div class="well" style="padding-bottom: 20px; margin: 0;">
		<span class="error pull-right"> * marks fields are mandatory</span>
		<h4>General Information</h4>

		<hr class="separator" />


		<div class="row-fluid">



			<div class="control-group">
				<label class="control-label" for="businesstitle">Name<span
					class="error">*</span>



				</label>
				<div class="controls">
					<span class="text-error"><?php echo form_error('name'); ?>
					</span> <input class="span6" id="name"
						name="name" type="text" placeholder="Name"/>
				</div>
			</div>
			
			


			<div class="control-group">
				<label class="control-label" for="password">Keyword<span
					class="error">*</span>



				</label>
				<div class="controls">

					<span class="text-error"> <?php echo form_error('keyword[]'); ?>
					</span>
					<div class="small">
						<span class="text-success">Select Multiple keyword <strong>CTRL+CLICK</strong>
						</span>
					</div>
					<select multiple="multiple" size=10
						style='height: 10%; width: 50%;' name="keyword[]" id="keyword"
						 onchange="return testSubmit()">
						<?php foreach($keyword as $keyword):?>
						<option value="<?= $keyword->id;?>"><?= $keyword->name;?></option>
						<?php endforeach;?>

					</select>
				</div>
			</div>
			
			<h4>Address Information</h4>
			<hr class="separator" />
			<div class="control-group">
				<label class="control-label" for="state">State <span class="error">*</span>
				</label>
				<div id="statediv" class="controls" onClick="getlatlong()">
					<span class="text-error"> <?php echo form_error('state'); ?>
					</span> <select  name="state" id="state"
						>
						
						<option value="">-- Select State --</option>
						<?php foreach($state as $state):?>
						<option value="<?= $state->id;?>"><?= $state->statename;?></option>
						<?php endforeach;?>
					</select>

				</div>

			</div>
			<div class="control-group">
				<label class="control-label" for="city">City<span class="error">*</span>



				</label>
				<div id="citydiv" class="controls" onClick="getlatlong()">
					<span class="text-error"> <?php echo form_error('city'); ?>
					</span> <select name="city" id="city"
						disabled="disabled">
						<option>-- Select City --</option>
						<option value=" "></option>
					</select>
				</div>
			</div>

			<div class="control-group">
				<label class="control-label" for="email">Zip Code<span class="error">*</span>



				</label>
				<div class="controls">
					<span class="text-error"> <?php echo form_error('zip_code'); ?>
					</span> <input class="span4" id="zip_code" name="zip_code"
						type="text" placeholder="Zip Code" />
				</div>
			</div>

			<div class="control-group">
				<label class="control-label" for="email">Address Line 1 <span
					class="error">*</span>



				</label>
				<div class="controls" onClick="getlatlong()">
					<span class="text-error"> <?php echo form_error('street_address'); ?>
					</span> <input class="span6" id="street_address"
						name="street_address" type="text" placeholder="Street Address 1" />
				</div>
			</div>
			<div class="control-group">
				<label class="control-label" for="email">Address Line 2</label>
				<div class="controls" onClick="getlatlong()">
					<input class="span6" id="street_address2" name="street_address2"
						type="text" placeholder="Street Address 2" />
				</div>
			</div>
			<div class="control-group" style="display: none" id="mymap">
				<label class="control-label" for="inputPassword">Select Place*</label>
				<div class="controls">
					<div id="map"></div>
				</div>
			</div>

			<div class="control-group">
				<label class="control-label" for="email">Latitude<span class="error">*</span>



				</label>
				<div class="controls">

					<input class="span4" id="lat" name="lat" type="text" /> Longitude <span
						class="error">*</span> <input class="span4" id="long" name="long"
						type="text" />


				</div>
			</div>
			<div class="control-group">
				<label class="control-label" for="email">E-mail</label>
				<div class="controls">

					<input class="span6" id="email" name="email"
						type="text" placeholder="Email" />
				</div>
			</div>
			<div class="control-group">
				<label class="control-label" for="email">Website Url</label>
				<div class="controls">

					<input class="span6" id="website_url" name="website_url"
						type="text" placeholder="Website Url" />
				</div>
			</div>

			<h4>Practice & Contact Information</h4>
			<hr class="separator" />

			<div class="control-group">
				<label class="control-label" for="email">Work Experience</label>
				<div class="controls">

					<input class="span4" id="work_experience" name="work_experience"
						type="text" placeholder="Work Experience" /> 
				</div>
			</div>
			<div class="control-group">
				<label class="control-label" for="email"> Education<span class="error">*</span></label>
				<div class="controls">
<span class="text-error"> <?php echo form_error('education'); ?>
					</span>
					<input class="span6" id="education"
						name="education" type="text"
						placeholder="Education" /> 
				</div>

			</div>
<div class="control-group">
				<label class="control-label" for="email">Awards</label>
				<div class="controls">

					<input class="span6" id="awards"
						name="awards" type="text"
						placeholder="Awards" /> 
				</div>

			</div>
			<div class="control-group">
				<label class="control-label" for="email">Associations</label>
				<div class="controls">

					<input class="span4" id="associations"
						name="associations" type="text"
						placeholder="Associations" /> 
				</div>

			</div>
			<div class="control-group">
				<label class="control-label" for="email">Language<span class="error">*</span></label>
				<div class="controls">
<span class="text-error"> <?php echo form_error('language'); ?>
					</span>
					<input class="span6" id="language"
						name="language" type="text"
						placeholder="Language" /> 
				</div>

			</div>
			<div class="control-group">
				<label class="control-label" for="email">Facebook</label>
				<div class="controls">

					<input class="span6" id="facebook"
						name="facebook" type="text"
						placeholder="Facebook" /> 
				</div>

			</div>
			<div class="control-group">
				<label class="control-label" for="email">Twitter</label>
				<div class="controls">

					<input class="span6" id="twitter"
						name="twitter" type="text"
						placeholder="Twitter" /> 
				</div>

			</div>
			<div class="control-group">
				<label class="control-label" for="email">Linkedin</label>
				<div class="controls">

					<input class="span6" id="linkedin"
						name="linkedin" type="text"
						placeholder="Linkedin" /> 
				</div>

			</div>
			
			<div class="control-group">
				<label class="control-label" for="email">Google+</label>
				<div class="controls">

					<input class="span6" id="google_plus"
						name="google_plus" type="text"
						placeholder="Google Plus" /> 
				</div>

			</div>
			
			
			<h4>About your Service</h4>
			<hr class="separator" />
			<div class="control-group">
				<label class="control-label" for="email">Description <span
					class="error">*</span>



				</label>
				<div class="controls">
					<span class="text-error"> <?php echo form_error('description'); ?>
					</span>
					<textarea rows=4 class="span12" name="description"></textarea>
				</div>
			</div>
			
		</div>
	</div>
	<div class="form-actions">

		<button type="submit"
			class="btn btn-icon btn-primary glyphicons circle_ok pull-right" <?php if($this->config->item('mode')=='DEMO'){?>
									onclick="alert ('Sorry! Not allowed in demo mode '); return false;"
									<?} else?> onclick="return confirm('Are you sure ?');">
			<i></i>Save Listing
		</button>
		<!-- <button type="button" class="btn btn-icon btn-default glyphicons circle_remove pull-right"><i></i>Cancel</button> -->
	</div>
</form>
<hr class="separator" />
<!-- End Content -->


<?php }?>
<?php 
if($mode=="edit"){
	?>
<script type="text/javascript">
$(document).ready(function(){
$('#mymap1').show();
var lat = <?php echo $listing['info']->lat; ?>;
var longg = <?php echo $listing['info']->long; ?>;
//alert (longg);

var latlng = new google.maps.LatLng(lat,longg);
    var map = new google.maps.Map(document.getElementById('map1'), {
        center: latlng,
        zoom: 14,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });
    var marker = new google.maps.Marker({
        position: latlng,
        map: map,
        title: 'Set lat/lon values for this property',
        draggable: true
    });
	 google.maps.event.addListener(marker, 'dragend', function(a) {
        console.log(a);
              
		document.getElementById('lat').value = a.latLng.lat().toFixed(4);
		document.getElementById('long').value = a.latLng.lng().toFixed(4);
        document.getElementsByTagName('body')[0].appendChild(div);
    });
	
});

function getlatlong() {
	$('#mymap1').show();
	
	var geocoder = new google.maps.Geocoder();
	
	var street=$("#street_address").val();
	var street2=$("#street_address2").val();
	var state=$("#statediv option:selected").html();
	var city=$("#citydiv option:selected").html();
	
	//var address=country+','+state+','+city+','+street;
	var address=street+','+street2+','+city+','+state;
	
	
	geocoder.geocode( { 'address': address}, function(results, status) {

	if (status == google.maps.GeocoderStatus.OK) {
    var latitude = results[0].geometry.location.lat();
	var longitude = results[0].geometry.location.lng();
	document.getElementById('lat').value=latitude;
	document.getElementById('long').value=longitude;
    } 

	
    var latlng = new google.maps.LatLng(latitude,longitude);
    var map = new google.maps.Map(document.getElementById('map1'), {
        center: latlng,
        zoom: 14,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });
    var marker = new google.maps.Marker({
        position: latlng,
        map: map,
        title: 'Set lat/lon values for this property',
        draggable: true
    });
    google.maps.event.addListener(marker, 'dragend', function(a) {
        console.log(a);
       
        
		document.getElementById('lat').value = a.latLng.lat().toFixed(4);
		document.getElementById('long').value = a.latLng.lng().toFixed(4);
        document.getElementsByTagName('body')[0].appendChild(div);
    });
	}); 
};
</script>

<style>
.inner {
	padding: 10px
}

#map1 {
	height: 300px;
	border: 1px solid #000;
}
</style>

<div class="separator"></div>

<h3 class="glyphicons briefcase">
	<i></i> Edit Your Business
</h3>
<div class="separator"></div>

<?php if($this->session->flashdata('message')) { 
				$show_message = $this->session->flashdata('message'); ?>
<span class="error"><?= $show_message; ?> </span>
<?php } ?>

<form class="form-horizontal" style="margin-bottom: 0;"
	name="edit_business" method="post"
	action="<?= base_url();?>secure/listing/listings/submit/edit/<?= $listing['info']->id;?>">


	<div class="well" style="padding-bottom: 20px; margin: 0;">
		<span class="error pull-right"> * marks fields are mandatory</span>
		<h4>General Information</h4>

		<hr class="separator" />


			
			
			
			<div class="row-fluid">

<div class="control-group">
				<label class="control-label" for="inputEmail">Mem Id </label>
				<div class="controls">
					<input class="input-small" type="text" name="mem_id" id="mem_id"
						placeholder="Mem ID" value="<?= $listing['info']->mem_id;?>">


				</div>
			</div>

			<div class="control-group">
				<label class="control-label" for="businesstitle">Name<span
					class="error">*</span>



				</label>
				<div class="controls">
					<span class="text-error"><?php echo form_error('name'); ?>
					</span> <input class="span6" id="name"
						name="name" type="text" value="<?= $listing['info']->name;?>"/>
				</div>
			</div>
			
			


			<div class="control-group">
				<label class="control-label" for="password">Keyword<span
					class="error">*</span>



				</label>
				<div class="controls">

					<span class="text-error"> <?php echo form_error('keyword[]'); ?>
					</span>
					<div class="small">
						<span class="text-success">Select Multiple keyword <strong>CTRL+CLICK</strong>
						</span>
					</div>
					<select multiple="multiple" size=10
										style='height: 10%; width: 50%;' name="keyword[]" id="keyword"
										onchange="return testSubmit()">
										<?php foreach($keywords as $keywords):?>


										<?php $key= $this->admin_listing_model->get_listing_keyword($listing['info']->id,$keywords->id);?>

										<option value="<?= $keywords->id;?>"
										<?php if($key == $keywords->id){
											echo "selected=selected";
											}?>>


											<?= $keywords->name;?>

										</option>
										<?php endforeach;?>
									</select>
				</div>
			</div>
			
			<h4>Address Information</h4>
			<hr class="separator" />
		<div class="control-group">
				<label class="control-label" for="state">State <span class="error">*</span>
				</label>
				<div id="statediv" class="controls" onClick="getlatlong()">
					<span class="text-error"> <?php echo form_error('business_state'); ?>
					</span> 
					
					
					
					<select name="state" id="state">
											<option>-- Select State --</option>
											<?php foreach($state['info'] as $state):?>
											<option value="<?= $state->id;?>"
											<?php if($state->id == $listing['info']->state){echo "selected=selected";}?>>
												<?= $state->statename;?>
											</option>
											<?php endforeach;?>
										</select>

				</div>

			</div>
			<div class="control-group">
				<label class="control-label" for="city">City<span class="error">*</span>



				</label>
				<div id="citydiv" class="controls" onClick="getlatlong()">
					<span class="text-error"> <?php echo form_error('business_city'); ?>
					</span> <select name="city" id="city">
											<option>-- Select City --</option>
											<?php foreach($city['info'] as $city):?>
											<option value="<?= $city->id;?>"<?php if($city->id == $listing['info']->city){echo "selected=selected";}?>" >
												<?= $city->cityname;?>
											</option>
											<?php endforeach;?>
										</select>
				</div>
			</div>

			<div class="control-group">
				<label class="control-label" for="email">Zip Code<span class="error">*</span>



				</label>
				<div class="controls">
					<span class="text-error"> <?php echo form_error('pin_code'); ?>
					</span> <input class="span4" id="zip_code" name="zip_code"
						type="text" value="<?= $listing['info']->zipcode;?>" />
				</div>
			</div>

			<div class="control-group">
				<label class="control-label" for="email">Address Line 1 <span
					class="error">*</span>



				</label>
				<div class="controls" onClick="getlatlong()">
					<span class="text-error"> <?php echo form_error('street_address'); ?>
					</span> <input class="span6" id="street_address"
						name="street_address" type="text" placeholder="Street Address 1" value="<?= $listing['info']->address_1; ?> "/>
				</div>
			</div>
			<div class="control-group">
				<label class="control-label" for="email">Address Line 2</label>
				<div class="controls" onClick="getlatlong()">
					<input class="span6" id="street_address2" name="street_address2"
						type="text" placeholder="Street Address 2" value="<?= $listing['info']->address_2;?>" />
				</div>
			</div>
			<div class="control-group" id="mymap1">
									<label class="control-label" for="inputPassword">Select Place*</label>
									<div class="controls">
										<div id="map1"></div>
									</div>
								</div>

			<div class="control-group">
				<label class="control-label" for="email">Latitude<span class="error">*</span>



				</label>
				<div class="controls">

					<input class="span4" id="lat" name="lat" type="text" value="<?= $listing['info']->lat;?>"/> Longitude <span
						class="error">*</span> <input class="span4" id="long" name="long"
						type="text" value="<?= $listing['info']->long;?>" />


				</div>
			</div>
			<div class="control-group">
				<label class="control-label" for="email">E-mail</label>
				<div class="controls">

					<input class="span6" id="email" name="email"
						type="text" value="<?= $listing['info']->email;?>" />
				</div>
			</div>
			<div class="control-group">
				<label class="control-label" for="email">Website Url</label>
				<div class="controls">

					<input class="span6" id="website_url" name="website_url"
						type="text" value="<?= $listing['info']->website;?>" />
				</div>
			</div>

			<h4>Practice & Contact Information</h4>
			<hr class="separator" />

			<div class="control-group">
				<label class="control-label" for="email">Work Experience</label>
				<div class="controls">

					<input class="span4" id="work_experience" name="work_experience"
						type="text" value="<?= $listing['info']->work_experience;?>" /> 
				</div>
			</div>
			<div class="control-group">
				<label class="control-label" for="email"> Education<span class="error">*</span></label>
				<div class="controls">
<span class="text-error"> <?php echo form_error('education'); ?>
					</span>
					<input class="span6" id="education"
						name="education" type="text"
						value="<?= $listing['info']->education;?>" /> 
				</div>

			</div>
<div class="control-group">
				<label class="control-label" for="email">Awards</label>
				<div class="controls">

					<input class="span6" id="awards"
						name="awards" type="text"
						value="<?= $listing['info']->awards;?>" /> 
				</div>

			</div>
			<div class="control-group">
				<label class="control-label" for="email">Associations</label>
				<div class="controls">

					<input class="span4" id="associations"
						name="associations" type="text"
						value="<?= $listing['info']->associations;?>"/> 
				</div>

			</div>
			<div class="control-group">
				<label class="control-label" for="email">Language<span class="error">*</span></label>
				<div class="controls">
<span class="text-error"> <?php echo form_error('language'); ?>
					</span>
					<input class="span6" id="language"
						name="language" type="text"
						value="<?= $listing['info']->language;?>" /> 
				</div>

			</div>
			<div class="control-group">
				<label class="control-label" for="email">Facebook</label>
				<div class="controls">

					<input class="span6" id="facebook"
						name="facebook" type="text"
						value="<?= $listing['info']->facebook;?>" /> 
				</div>

			</div>
			<div class="control-group">
				<label class="control-label" for="email">Twitter</label>
				<div class="controls">

					<input class="span6" id="twitter"
						name="twitter" type="text"
						value="<?= $listing['info']->twitter;?>" /> 
				</div>

			</div>
			<div class="control-group">
				<label class="control-label" for="email">Linkedin</label>
				<div class="controls">

					<input class="span6" id="linkedin"
						name="linkedin" type="text"
						value="<?= $listing['info']->linkedin;?>"/> 
				</div>

			</div>
			
			<div class="control-group">
				<label class="control-label" for="email">Google+</label>
				<div class="controls">

					<input class="span6" id="google_plus"
						name="google_plus" type="text"
						<?= $listing['info']->google_plus;?>/> 
				</div>

			</div>
			
			
			<h4>About your Service</h4>
			<hr class="separator" />
			<div class="control-group">
				<label class="control-label" for="email">Description <span
					class="error">*</span>



				</label>
				<div class="controls">
					<span class="text-error"> <?php echo form_error('description'); ?>
					</span>
					<textarea rows=4 class="span12" name="description"><?= $listing['info']->about;?></textarea>
				</div>
			</div>
			
		</div>

									
	<div class="form-actions">

		<button type="submit"
			class="btn btn-icon btn-primary glyphicons circle_ok pull-right" <?php if($this->config->item('mode')=='DEMO'){?>
									onclick="alert ('Sorry! Not allowed in demo mode '); return false;"
									<?} else?> onclick="return confirm('Are you sure ?');">
			<i></i>Update
		</button>
		<!-- <button type="button" class="btn btn-icon btn-default glyphicons circle_remove pull-right"><i></i>Cancel</button> -->
	</div>
</form>
<hr class="separator" />

<?php }?>





